#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>


using namespace std;

// 1353. 最多可以参加的会议数目
// https://leetcode.cn/problems/maximum-number-of-events-that-can-be-attended/description/?envType=daily-question&envId=2025-07-07

// 错误
class Solution1
{
public:
    bool isStandard(vector<vector<int>> &events, int cnt)
    {
        int curCnt = 0;
        int pre = -1;
        for (int i = 0; i < events.size(); i++)
        {
            if (pre == -1)
            {
                pre = events[i][0];
                curCnt++;
                continue;
            }

            if (events[i][1] > pre)
            {
                if (events[i][0] > pre)
                {
                    pre = events[i][0];
                    curCnt++;
                }
                else
                {
                    pre += 1;
                    curCnt++;
                }
            }

            if (curCnt >= cnt)
            {
                return true;
            }
        }
        return curCnt >= cnt;
    }
    int maxEvents(vector<vector<int>> &events)
    {
        sort(events.begin(), events.end(), [](vector<int> &a, vector<int> &b)
             { return a[0] < b[0]; });
        int ans = 0;
        int left = 0;
        int right = events.size();
        while (left <= right)
        {
            int mid = (right - left) / 2 + left;
            if (isStandard(events, mid))
            {
                ans = mid;
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }
        return ans;
    }
};

class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        int maxCap = 0;
        for(auto &nums : events){
            maxCap = max(maxCap, nums[1]);
        }

        priority_queue<int, vector<int>, greater<int>> pq;
        vector<vector<int>> group(maxCap + 1);

        for(auto &nums : events){
            group[nums[0]].push_back(nums[1]);
        }

        int ans = 0;
        for (int i = 0; i <= maxCap; i++)
        {
            while(!pq.empty() && pq.top() < i){
                pq.pop();
            }
            for(auto endDay : group[i]){
                pq.push(endDay);
            }
            if(!pq.empty()){
                ans++;
                pq.pop();
            }
        }
        return ans;
    }
};

int main()
{
    vector<vector<int>> events{{1, 2}, {2, 3}, {3, 4}};
    Solution solution{};
    solution.maxEvents(events);
    return 0;
}